package com.toolbar;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class FloatingAction extends AppCompatActivity {
    private static final String TAG = "FloatingAction";
    private FloatingActionButton mFab;
    private LinearLayout bottomSheet;
    private BottomSheetBehavior<LinearLayout> bottomSheetBehavior;
    private boolean isOpen = false;
    private BottomSheetDialog mBottomSheetDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_floating_action);

        mFab = findViewById(R.id.fab);
        bottomSheet = findViewById(R.id.bottom_sheet);
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(mFab, "我是snackbar", Snackbar.LENGTH_LONG).setAction("点击", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(FloatingAction.this, "Snackbar点击事件", Toast.LENGTH_SHORT).show();
                    }
                }).show();
                if (isOpen) {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                } else {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
                isOpen = !isOpen;
            }
        });


        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View view, int i) {
                Toast.makeText(FloatingAction.this, "newState: " + i, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSlide(@NonNull View view, float v) {
                Log.e(TAG, "onSlide: " + v);
            }
        });

        findViewById(R.id.btn_BottomSheetDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //第一种
                mBottomSheetDialog = new BottomSheetDialog(FloatingAction.this);
                mBottomSheetDialog.setContentView(R.layout.bottom_sheet);
                mBottomSheetDialog.show();

                //第二种
//                new MyBSDFragment().show(getSupportFragmentManager(), "toly");
            }
        });
    }
}

